<template>
	<view>
		<s-layout title="商品库存" navbar="custom1" :bgStyle="{ color: '#F1F1F1' }">
			<assNavbar class="ss-m-b-10" title="扫码核销"  
			:background="{img: 'none',BGcolor: ' linear-gradient(180deg, #7881d9 0%, #f0b8ea 100%)'}"  
			></assNavbar>
			<view class="text-center">
				<image
				  :src="sheep.$url.cdn('/static/merchant/shop/icon-scan.png')"
				  class="scan-btn ss-m-y-60"
				  @tap="openscan"
				></image>
			</view>
			<view class="bg-white ss-flex ss-p-y-10 ss-p-x-20">
				<view class="icon"></view>
				<view class="font-1 ss-m-l-20">核销记录</view>
			</view>
			
			<scroll-view
			  class="list-scroll"
			  scroll-y="true"
			  :style="{height:(windowHeight-sys_navBar-250)*2+'rpx'}"
			  @scrolltolower="log()"
			>
				<view class="ss-p-20">
					<view
					  class="log-item bg-white ss-p-y-10 ss-p-x-20"
					  v-for="(item,index) in state.list"
					  :key="index"
					>
					    <view class="ss-flex justify-between item">
							 <view>优惠券ID</view>
							 <view>{{item.code}}</view>
						 </view>
						 <view class="ss-flex justify-between item">
							 <view>核销用户</view>
							 <view>{{item.use_user?.nickname}}</view>
						 </view>
						 <view class="ss-flex justify-between item">
							 <view>购买时间</view>
							 <view>{{item.create_time}}</view>
						 </view>
						 <view class="ss-flex justify-between item">
							 <view>核销时间</view>
							 <view>{{item.use_time}}</view>
						 </view>
					</view>
				</view>
			  
			</scroll-view>
			
			
			
			
		</s-layout>
	</view>
</template>

<script setup>
//附近门店列表页
import {
		computed,
		reactive,
		onBeforeMount,
		ref
	} from 'vue';
import {
	onLoad,
	onReachBottom,
	onPageScroll,
	onReady
} from '@dcloudio/uni-app';
import sheep from '@/sheep';
import _ from 'lodash'
import assNavbar from '@/components/ass-navbar.vue'
import couponAPI from "@/ygb/api/coupon"
import {
		queryToParams
	} from '@/sheep/helper/index.js';
const sys_navBar = sheep.$platform.navbar;//标题栏高度
const windowHeight = sheep.$platform.device.windowHeight; // 获得窗口高度

const state = reactive({
	list:[],
	page:0,
	last_page:1
})

// 核销
async function openscan(){
	uni.scanCode({
		success: async (res) => {
			let query = queryToParams(decodeURIComponent(res.result).split('?')[1]);
			console.log('res,query',res,query)
			if (query.code) {
	
				const res = await couponAPI.ticket_scan(query.code);
	
				if (res.error == 0) {
					uni.showToast({
						title: res.msg,
						icon: 'none',
						duration:3000
					});
				}
			}
		},
	});
}
// 核销记录
async function log(page){
	
	if(state.last_page>state.page){
		state.page=state.page+1
		if(page){
			state.page=page
		}
		const res = await couponAPI.ticket_list({
			page:state.page, 
			status:1
		});
		if(res.error==0){
			state.last_page=res.data.last_page
			if(state.page==1){
				state.list=res.data.data
			}else{
				state.list=state.list.concat(res.data.data)
			}
		}
	}
	
}
onLoad(async ()=>{
	await log()
})
</script>


<style lang="scss" scoped>
	view{
		box-sizing: border-box;
		font-family: Source Han Sans; 
	}
	
	
	
	.font-w{
		font-weight: bold;
	}
	.scan-btn{
		width:400rpx;
		height:400rpx;
		border-radius: 50%;
		
	}
	.icon{
		width: 6rpx;
		height: 40rpx;
		
		/* 主题色 */
		background: linear-gradient(180deg, #7881D9 0%, #F0B8EA 100%);
	}
	.log-item{
		font-size: 28rpx;
		border-radius:20rpx;
		color: #3D3D3D;
		margin-bottom: 20rpx;
		.item{
			padding:10rpx;
			border-bottom: 2rpx solid #f1f1f1;
		}
	}
</style>
